// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html
cc.Class({
    extends: cc.Component,

    properties: {
        className:"userView",
        qiangzhuangframe:{
            default:null,
            type:cc.SpriteFrame,
        },
        status_ready:{
            default:null,
            type:cc.SpriteFrame,
        },
        buqiangframe:{
            default:null,
            type:cc.SpriteFrame,
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad: function () {
        this.node.thisJS = this;
        this.initUI();
    },
    //更新头像位置
    updateUI:function(subConfig){
        this.initUI();
        for(var k in subConfig){
            console.log("k->"+k);
            this[k].setPosition(subConfig[k]);
        }
    },
    initUI: function(){
        this.head = cc.find("headImg",this.node);
        this.name = cc.find("name",this.node);
        this.zhuang = cc.find("zhuang",this.node);
        this.score = cc.find("score",this.node);
        this.statuslayout = cc.find("statuslayout",this.node);
        this.state_img = cc.find("status_ready",this.statuslayout);
        this.beiscore = cc.find("beiscore",this.statuslayout);
        this.bistate = cc.find("bistate",this.node);
        this.mask = cc.find("mask",this.node);
        this.state_img.active = false ;
        this.zhuang.active  = false ;
        this.mask.active = false ;
        if(this.bistate){
            this.bistate.active = false ;
            this.orgPos = this.bistate.getPosition();
        }

        this.timernode = cc.find("timer",this.node);
        if(this.timernode){
           this.timerlabel = cc.find("label",this.timernode);
           this.timernode.active = false ;
        }
        
    //     var info = {
    //         score:10,
    //         name:"tlx",
    //     }
    //     this.initUserData(info);
     },  

    //更新金币
    updateUserScore: function(score){
        this.score.getComponent(cc.Label).string = score ;//Util.getFormatString(score, 2) ;
    },

    updateBiState:function(state){
        if(state){
            var moveby1 = cc.moveBy(0.5,cc.p(0,25))
            var moveby2 = cc.moveBy(0.5,cc.p(0,-25))
            var seq = cc.sequence(moveby1,moveby2);
            var ac = cc.repeatForever(seq);
            this.bistate.active = true ;
            this.bistate.runAction(ac);
        }else{
            this.bistate.stopAllActions();
            this.bistate.setPosition(this.orgPos); 
            this.bistate.active = false ;    
        }
    },
    //初始化数据
    initUserData: function(info){
        this.updateUserScore(info.score || 0);
        this.name.getComponent(cc.Label).string = info.nick;
        this.updateReadyStatus(info.status==GameConfig.playerStatus.USER_STATE_ALREADY_READY);
    },
    
    //庄家设置
    updateDealer: function(state){
        this.zhuang.active = state;
    },

    hideStatus:function(){
        console.log("hideStatus---------------");
        this.scheduleOnce(function(){
             this.state_img.active = false ;
             this.beiscore.active = false ;
        },0.1)
    },

    //更新状态
    updateReadyStatus:function(state){
        var self = this ;
        self.state_img.getComponent(cc.Sprite).spriteFrame = this.status_ready;
        self.state_img.active = state ;
        console.log("updateReadyStatus---------------");
    },
    //更新抢的状态
    updateQiangStatus:function(bei){
        var self = this ;
        self.state_img.getComponent(cc.Sprite).spriteFrame = this.qiangzhuangframe;
        self.state_img.active = true ;
        if(bei){
            self.beiscore.active = true ;
            self.beiscore.getComponent(cc.Label).string = bei +"分";
        }
        console.log("updateQiangStatus---------------");
    },
    //更新抢的状态
    updateBuQiangStatus:function(){
        var self = this ;
        self.state_img.getComponent(cc.Sprite).spriteFrame = this.buqiangframe;
        console.log("updateBuQiangStatus---------------");
        self.state_img.active = true ;
    },
    playZhuangAnim:function(){
        console.log("播放定庄动画");
        this.zhuang.active  = true;
        var pos = this.node.convertToNodeSpace(cc.p(750/2,1280/2));
        var orgPos = this.zhuang.getPosition();
        this.zhuang.setPosition(pos);
        var delay = cc.delayTime(0.5);
        var move  = cc.moveTo(0.5,orgPos);
        var sq    = cc.sequence(delay,move);

        this.zhuang.runAction(sq);
    },
    updateMask:function(state){
        this.mask.active = state ;
    },
    clearTable:function(){ 
        this.state_img.active = false ;
        //if(GameData.play_type != 2 && GameData.play_type != 1){
        this.zhuang.active = false ;
        if(this.bistate){
            this.bistate.active = false ;
        }
    },
    startTimer:function(time)
    {   
        this.timernode.active = true ;
        this.timerlabel.getComponent(cc.Label).string  = time + "";
        if(this._timecount_id){
            this.unschedule(this._timecount_id);
            this._timecount_id = null;
        }
        this._timecount_id = function (){     
            time =time -1;    
            if(time<0){
                this.unschedule(this._timecount_id);
                this._timecount_id = null;
                this.timernode.active = false;
            }
            else{
                this.timerlabel.getComponent(cc.Label).string  = time + "";
            }  
        }
        this.schedule(this._timecount_id,1);
    },
    stopTimer:function(){
        if (this._timecount_id ){
           this.unschedule(this._timecount_id);
           this._timecount_id = null;
        }
        this.timernode.active = false ;
    },
});
